package org.example.viteVantDemo.security;


import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.Collections;

/**
 * 实现Spring Security的UserDetails接口
 *
 * @author tnar
 */
public class VantUserDetails implements UserDetails {
    /**
     * 系统用户信息
     **/
    private VantUser sysUser;

    /**
     * 密码
     **/
    private String credential;

    /**
     * 角色+权限 集合   （角色必须以： ROLE_ 开头）
     **/
    private Collection<SimpleGrantedAuthority> authorities = Collections.emptyList();

    /**
     * 缓存标志
     **/
    private String cacheKey;

    /**
     * 登录IP
     **/
    private String loginIp;

    //此处的无参构造，为json反序列化提供
    public VantUserDetails() {
    }

    public VantUserDetails(VantUser sysUser, String credential) {

        this.setSysUser(sysUser);
        this.setCredential(credential);
        //做一些初始化操作
    }

    /**
     * spring-security 需要验证的密码
     **/
    @Override
    public String getPassword() {
        return getCredential();
    }

    /**
     * spring-security 登录名
     **/
    @Override
    public String getUsername() {
        return getSysUser().getUsername();
    }

    /**
     * 账户是否过期
     **/
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     * 账户是否已解锁
     **/
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    /**
     * 密码是否过期
     **/
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /**
     * 账户是否开启
     **/
    @Override
    public boolean isEnabled() {
        return true;
    }

    /**
     * 获取权限集合
     **/
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    public VantUser getSysUser() {
        return sysUser;
    }

    public void setSysUser(VantUser sysUser) {
        this.sysUser = sysUser;
    }

    public String getCredential() {
        return credential;
    }

    public void setCredential(String credential) {
        this.credential = credential;
    }

    public String getCacheKey() {
        return cacheKey;
    }

    public void setCacheKey(String cacheKey) {
        this.cacheKey = cacheKey;
    }

    public String getLoginIp() {
        return loginIp;
    }

    public void setLoginIp(String loginIp) {
        this.loginIp = loginIp;
    }
}
